package com.cn.dlwy.mapzone.activity;

import com.cn.dlwy.mapzone.R;
import com.cn.dlwy.mapzone.R.drawable;
import com.cn.dlwy.mapzone.R.id;
import com.cn.dlwy.mapzone.R.layout;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private WindowManager wm = null;
	private WindowManager.LayoutParams wmParams = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// 初始化悬浮窗口
		initFloatView();
	}

	/**
	 * 初始化悬浮按钮
	 */
	private void initFloatView() {
		// 获取WindowManager
		wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		// 设置LayoutParams(全局变量）相关参数
		wmParams = new WindowManager.LayoutParams();

		wmParams.type = LayoutParams.TYPE_PHONE; // 设置window type phone为当前屏幕优先级与电话优先级相同
		wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式，效果为背景透明
		// 设置Window flag
		wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
				| LayoutParams.FLAG_NOT_FOCUSABLE;

		// 以屏幕左上角为原点，设置x、y初始值
		wmParams.x = 10;
		wmParams.y = 0;
		// 设置悬浮窗口长宽数据
		wmParams.width = 50;
		wmParams.height = 50;

		// 创建悬浮按钮
		createLeftFloatView();
	}

	/**
	 * 创建左边悬浮按钮
	 */
	private void createLeftFloatView() {
		final ImageView leftbtn = new ImageView(this);
		leftbtn.setImageResource(R.drawable.ic_launcher);
		leftbtn.setAlpha(150);
		leftbtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT)
						.show();
				wm.removeView(leftbtn);
			}
		});
		// 调整悬浮窗口
		wmParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
		// 显示myFloatView图像
		wm.addView(leftbtn, wmParams);
	}
}
